package service;

import util.DateUtil;
import util.OpentsUtil;

import java.sql.*;

/**
 * @Auther: wy
 * @Date: 2018/9/25 10:40
 * @Description:
 */
public class OdsDataAlarm {
    private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    private static String url = "jdbc:hive2://192.168.1.61:10000/ods_tantu_map_log";

    private static Connection conn = null;
    private static Statement stmt = null;
    private static ResultSet rs = null;

    // 加载驱动、创建连接
    public void init() throws Exception {
        Class.forName(driverName);
        conn = DriverManager.getConnection(url);
        stmt = conn.createStatement();
    }

    // 释放资源
    public void destory() throws Exception {
        if (rs != null)
            rs.close();
        if (stmt != null)
            stmt.close();
        if (conn != null)
            conn.close();
    }


    // tantu_map_business（会运行mapreduce作业）
    public int tantuMapBusinessCountData() throws Exception {
        String sql = "SELECT count(1) from tantu_map_business WHERE day = '" + DateUtil.getDay() + "' AND hour = '" + DateUtil.getHour() + "'";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        int count = 0;
        while (rs.next()) {
            count = rs.getInt(1);
        }
        destory();
        return count;
    }

    // tantu_map_performance（会运行mapreduce作业）
    public int tantuMapPerformanceCountData() throws Exception {
        String sql = "SELECT count(1) from tantu_map_performance WHERE day = '" + DateUtil.getDay() + "' AND hour = '" + DateUtil.getHour() + "'";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        int count = 0;
        while (rs.next()) {
            count = rs.getInt(1);
        }
        destory();
        return count;
    }

    // tantu_map_unknown（会运行mapreduce作业）
    public int tantuMapUnknownCountData() throws Exception {
        String sql = "SELECT count(1) from tantu_map_unknown WHERE day = '" + DateUtil.getDay() + "' AND hour = '" + DateUtil.getHour() + "'";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        int count = 0;
        while (rs.next()) {
            count = rs.getInt(1);
        }
        destory();
        return count;
    }

    // tantu_map_user_action（会运行mapreduce作业）
    public int tantuMapUserActionCountData() throws Exception {
        String sql = "SELECT count(1) from tantu_map_user_action WHERE day = '" + DateUtil.getDay() + "' AND hour = '" + DateUtil.getHour() + "'";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        int count = 0;
        while (rs.next()) {
            count = rs.getInt(1);
        }
        destory();
        return count;
    }

    // tantu_map_user_page（会运行mapreduce作业）
    public int tantuMapUserPageCountData() throws Exception {
        String sql = "SELECT count(1) from tantu_map_user_page WHERE day = '" + DateUtil.getDay() + "' AND hour = '" + DateUtil.getHour() + "'";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        int count = 0;
        while (rs.next()) {
            count = rs.getInt(1);
        }
        destory();
        return count;
    }

    public static void main(String[] args) throws Exception {
        OdsDataAlarm odsDataAlarm = new OdsDataAlarm();
        odsDataAlarm.init();
        OpentsUtil.opentsPut("tantu_map", odsDataAlarm.tantuMapBusinessCountData(), "tantu_map_business");
        odsDataAlarm.init();
        OpentsUtil.opentsPut("tantu_map", odsDataAlarm.tantuMapPerformanceCountData(), "tantu_map_performance");
        odsDataAlarm.init();
        OpentsUtil.opentsPut("tantu_map", odsDataAlarm.tantuMapUnknownCountData(), "tantu_map_unknown");
        odsDataAlarm.init();
        OpentsUtil.opentsPut("tantu_map", odsDataAlarm.tantuMapUserActionCountData(), "tantu_map_user_action");
        odsDataAlarm.init();
        OpentsUtil.opentsPut("tantu_map", odsDataAlarm.tantuMapUserPageCountData(), "tantu_map_user_page");

    }


}
